/**
 * Created by duzi on 6/30/15.
 */
$(document).ready(function(){
    var $product = $('#productId');
    var $itemBtn = $('#itemBtn');
    var $orderBtn = $("#orderBtn");
    var $productInfo = $("#productDetail");
    var $list = $('#items');
    var $amount = $('#amount');
    var orderList = [];
    var tmpInfo = '';
    var $successList = $('.orderList');
    var $successPanel = $('#orderSuccessPanel');
    var $billSum = $('#billSum');

    $product.on('keyup', function(){
        var productId = $(this).val();
        if(!productId){
            return;
        }
        if(!productId.match(/[0-9]*/)){
            return;
        }

        $.ajax({
            type: "GET",
            url: "/products/" + productId,
            contentType: 'application/json',
            success: function(response){
                tmpInfo = response;
                $productInfo.html(response.productId + ' | ' + response.productName + ' | ' + response.productPrice + ' | ' + tmpInfo.productStock );
            }
        })
    })

    $amount.on('keyup', function(){
        if(this.value > tmpInfo.productStock){
            alert('dont have so much~');
        }
    })
    $itemBtn.on('click', function(){

        if($amount.val() > tmpInfo.productStock){
            alert('dont have so much~');
            return;
        }

        var sumPrice = (tmpInfo.productPrice * $amount.val());
        tmpInfo.productStock -= $amount.val();
        $list.append("<li>" + tmpInfo.productId + ' | ' + $amount.val() + ' | ' + sumPrice + ' | ' + tmpInfo.productStock + "</li>");
        var date = new Date();

        orderList.push({
            createTime: date.getTime(),
            sum: $amount.val(),
            price: sumPrice,
            productRef: tmpInfo.productId,
        });


    })

    $orderBtn.on('click', function(){
        if(orderList.length == 0){
            return;
        }
        $.ajax({
            type: "POST",
            url: "/bills",
            data: JSON.stringify({items: orderList}),
            contentType: 'application/json',
            success: function(response){
                $list.html("");
                tmpInfo = null;
                orderList = [];
                setBillPannel(response.id);
            }
        })
    });

    $successList.on('click', 'span', function(){
        $.ajax({
            type: "DELETE",
            url: "/billItems",
            data: JSON.stringify({items: [$(this).parent().data('id')]}),
            contentType: 'application/json',
            success: function(response){
                console.log(response);
                setBillPannel($successList.data('id'))
            }
        });
    })

    function setBillPannel(id){
        $.ajax({
            type: "GET",
            url: "/bills/"  + id,
            contentType: 'application/json',
            success: function(response){
                $successList.html("").data('id', response.billId);
                response.billItems.forEach(function(item,index){
                    $successList.append('<li data-id='+ item.billItemId +'>'
                        + item.product.productName + ' | ' + item.billItemSum
                        + ' | ' + item.billItemSumPrice
                        + '<span style="float:right">x</span></li>')

                });
                $billSum.html('<p>' + response.billId + ' | ' + response.billStatus + ' | ' + (response.billItems.length + 1) + '</p>')
            }
        })
    }
})
